package primesoft.cm.ui.bean;

import java.io.Serializable;

import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;

import oracle.binding.OperationBinding;

public class LanguageBean implements Serializable {
    
    String userLocale;
    
    
    public LanguageBean() {
        userLocale = Locale.getDefault().getLanguage();
    }

    public void setUserLocale(String userLocale) {
        this.userLocale = userLocale;
    }

    public String getUserLocale() {
        return userLocale;
    }
    
    private void changeLocale(String language){
        System.out.println("changeLocale "+language);
        this.userLocale = language;
        Locale newLocale = new Locale(this.userLocale);
        FacesContext context = FacesContext.getCurrentInstance();
        context.getViewRoot().setLocale(newLocale);
        
        // refresh lov method action
        //DCBindingContainer  bc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        //OperationBinding lovOper = bc.getOperationBinding("getLovData");
        //lovOper.execute();

    }
    
    /*
    public void localeChangeListener(ValueChangeEvent valueChangeEvent) {
        changeLocale(valueChangeEvent.getNewValue().toString());
    }
    */

    /**
     * @param actionEvent
     */
    public void localeChangeListener(ActionEvent actionEvent) {        
        String id = actionEvent.getComponent().getId();  
        changeLocale(id.substring(id.lastIndexOf("_")+1));
    }
}
